import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-03-01
 * Time:16:56
 */
public class Main {
    public static int count(int n){
        if (n==0 || n==1)return 0;
        Stack<Integer> fib = new Stack<>();
        int prev = 0;
        int top = 1;
        fib.push(prev);
        fib.push(top);
        while (top < n)
        {
            fib.push(prev+top);
            prev = top;
            top = fib.peek();
        }
        if (top == n)return 0;
        if (top - n >= n - prev){
            return n-prev;
        }
        return top-n;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            System.out.println(count(a));
        }
    }

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        HashSet<String> set = new HashSet<>();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.next();
            set.add(s);
        }
        System.out.println(set.size());
    }
}
